package me.qingy.programmatic;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
 * 居然卡壳
 *
 * @author qingy
 * @since 2021/8/19
 */
public class ReverseLeftWords {

    /**
     * 新建空数组填充
     * 时间复杂度 O(n) 空间复杂度O(n)
     */
    public String reverseLeftWords1(String s, int n) {
        char[] chars = new char[s.length()];
        // 填充 n~length
        for (int i = 0; i < n; i++) {
            chars[i + (chars.length -  n)] = s.charAt(i);
        }
        // 填充 0~n
        for (int i = 0; i < chars.length - n; i++) {
            chars[i] = s.charAt(i + n);
        }
        return new String(chars);
    }

    /**
     * 每次将一个元素不停向右移动一位
     * 时间复杂度 O(k^n) 空间复杂度(1) k 即 i
     */
    public String reverseLeftWords2(String s, int n) {
        char[] chars = s.toCharArray(); // 转换输入格式

        for (int i = 0; i < n; i++) {
            char tmp = chars[0];
            for (int j = 1; j < chars.length; j++) {
                chars[j - 1] = chars[j];
            }
            chars[chars.length - 1] = tmp;
        }
        return new String(chars);
    }




    @Test
    public void testReverseLeftWords() {
        Assertions.assertEquals("cdefgab", reverseLeftWords1("abcdefg", 2));
        Assertions.assertEquals("umghlrlose", reverseLeftWords1("lrloseumgh", 6));
    }
}
